<template>
  <div class="signin">
    <el-input clearable v-model="username" placeholder="用户名"></el-input>
    <el-input type="password" v-model="password" placeholder="密码"></el-input>
    <el-button @click="signinHandle">登录</el-button>
  </div>
</template>

<script>
import { Input, Button, Message } from 'element-ui';
import axios from '../helper/axios';

export default {
  components: {
    'el-input': Input,
    'el-button': Button,
  },
  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    signinHandle() {
      if (this.username === '') return Message.warning('你没有写用户名');
      if (this.password === '') return Message.warning('你没有写密码');

      axios.post('/api/signin', {
        username: this.username,
        password: this.password,
      }).then((res) => {
        localStorage.setItem('__token__', res.token);
        this.$router.replace('/');
      }).catch((err) => {
        Message.warning(err);
      });
    },
  },
};
</script>

<style>

</style>
